package com.app.service.impl;

/*
  @author 阿伟
  @create 2020-11-24:15:04
  逻辑层的复杂方法实现
*/

import com.app.condition.CompliantCondition;
import com.app.dao.CompliantDao;
import com.app.dao.UserDao;
import com.app.service.CompliantService;
import com.app.service.OssService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Code;
import com.common.entity.Compliant;
import com.common.entity.ResponseResult;
import com.common.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.regex.Pattern;

@Service
@Transactional
public class CompliantServiceImpl extends ServiceImpl<CompliantDao, Compliant> implements CompliantService {

    @Autowired
    CompliantDao compliantDao;

    @Autowired
    OssService service;

    @Autowired
    UserDao userDao;

    @Override
    public void selectCompliantByLimit(Page<Compliant> page, CompliantCondition compliantCondition) {


        //分页查询条件器
        QueryWrapper<Compliant> queryWrapper = new QueryWrapper<>();
        //判断是否有查询的条件
        if(compliantCondition == null){
            //没有条件则返回普通分页查询
            baseMapper.selectPage(page,queryWrapper);
            return;
        }

        //查询的条件
        String userNickname = compliantCondition.getUserNickname();
        String feedbacktype = compliantCondition.getCompliant_feedbacktype();
        String createTime = compliantCondition.getCreateTime();
        String modifyTime = compliantCondition.getModifyTime();
        //判断条件是否为空
        if(!StringUtils.isEmpty(userNickname)){
            queryWrapper.like("user_id",userNickname);
        }
        if(!StringUtils.isEmpty(feedbacktype)){
            queryWrapper.like("compliant_feedbacktype",feedbacktype);
        }
        if(!StringUtils.isEmpty(createTime)){
            queryWrapper.like("compliant_createdate",createTime);
        }
        if(!StringUtils.isEmpty(modifyTime)){
            queryWrapper.like("compliant_updatedate",modifyTime);
        }
        //分页和查询条件
        baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public ResponseResult addCompliant(String phone, String comtype, String question, MultipartFile img) {
        /**
         * 正则表达式：验证手机号
         */
        final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        //验证
        if(!Pattern.matches(REGEX_MOBILE,phone)){
            return ResponseResult.error(Code.REQUEST_ERROR).Data("error","输入正确的手机号");
        }
        //特殊判空
        if(org.apache.commons.lang.StringUtils.isBlank(comtype)
        || org.apache.commons.lang.StringUtils.isBlank(question)){
            return ResponseResult.error(Code.REQUEST_ERROR).Data("error","请求参数错误");
        }else{
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_phone",phone);
            User user = userDao.selectOne(wrapper);
            String url = null;
            //图片上传
            if(img != null){
                 url = service.uploadAliyun(img);
            }
            Compliant compliant = new Compliant();
            compliant.setCompliantId(0);
            compliant.setUserId(user.getUserId());
            compliant.setCompliantFeedbacktype(comtype);
            compliant.setCompliantQuestion(question);
            compliant.setCompliantImg(url);
            compliant.setCompliantLogicflage(0);
            compliant.setCompliantVersion(1);
            compliant.setCompliantCreatedate(null);
            compliant.setCompliantUpdatedate(null);
            int insert = compliantDao.insert(compliant);
            if(insert != 1){
                return ResponseResult.error(Code.REQUEST_ERROR).Data("msg","插入失败");
            }
            //如果用户有上传图片，那么返回上传图片后的地址
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("imgUrl",url);
        }
    }

    @Override
    public ResponseResult getAllCompliant() {
        List<Compliant> result = compliantDao.selectAllCompliant();
        if(result != null){
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("pliant",result);
        }
        return ResponseResult.error(Code.REQUEST_ERROR).Data("msg",null);
    }
}
